package com.jade.zsy.controller.purchase.api;

import com.jade.bss.base.RoleAction;
import com.jade.bss.base.utils.CopyBean;
import com.jade.framework.base.context.ApplicationContextUtils;
import com.jade.framework.base.util.PagingResult;
import com.jade.framework.base.util.ResponseUtils;
import com.jade.zsy.base.common.ParametersException;
import com.jade.zsy.base.entity.Employee;
import com.jade.zsy.pur.entity.PO;
import com.jade.zsy.pur.service.PODetailFacade;
import com.jade.zsy.pur.service.POFacade;
import com.jade.zsy.pur.vo.POVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: jinya
 * Date: 2017/6/5
 * Time: 15:18
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
@RestController
@RequestMapping(value = "/po", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "采购订单", description = "采购模块-采购订单")
public class POApi {
    private POFacade entityFacade = ApplicationContextUtils.getBean("POFacade");

    @RequestMapping(value =  RoleAction.QUERY, method = RequestMethod.GET)
    @ApiOperation(value = "采购订单", httpMethod = "GET", response = POVo.class)
    public void query(@ApiParam(value = "起始值") @RequestParam(value = "offset", defaultValue = "0") int offset,
                      @ApiParam(value = "每页大小") @RequestParam(value = "length", defaultValue = "10") int length,
                      @ApiParam(value = "关键词") @RequestParam (value = "keyword", required = false) String keyword,
                      @ApiParam(value = "排序字段") @RequestParam(value = "sort", required = false) String sort,
                      @ApiParam(value = "排序规则", allowableValues = "desc, asc") @RequestParam(value = "order", required = false) String order,
                      HttpServletResponse response, POVo employeeVo) throws Exception {
        PagingResult result = entityFacade.queryEntity(employeeVo, offset, length, keyword, sort);
        ResponseUtils.writeSuccessPagingResult(response, result);
    }

    //@RequiresAuthentication
    @RequestMapping(value = RoleAction.ADD, method = RequestMethod.POST)
    @ApiOperation(value = "新增采购订单", httpMethod = "POST")
    public void add(@ApiParam(value = "采购订单对象", required = true) @Validated @RequestBody POVo obj,
                    Errors errors, HttpServletResponse response) throws Exception {
        if (errors.hasErrors()) {
            FieldError field = errors.getFieldError();
            throw new ParametersException(field.getDefaultMessage(), field.getDefaultMessage() + ":" + field.getField(), null, field.getField());
        }
        PO entity = CopyBean.copy(obj, PO.class);
        entityFacade.save(entity);
        ResponseUtils.writeSuccessResult(response);
    }

    //@RequiresAuthentication
    @RequestMapping(value = RoleAction.UPDATE, method = RequestMethod.PUT)
    @ApiOperation(value = "修改采购订单", httpMethod = "PUT")
    public void update(@ApiParam(value = "修改采购订单", required = true) @Validated @RequestBody POVo obj,
                       Errors errors, HttpServletResponse response) throws Exception {
        if (errors.hasErrors()) {
            FieldError field = errors.getFieldError();
            throw new ParametersException(field.getDefaultMessage(), field.getDefaultMessage() + ":" + field.getField(), null, field.getField());
        }
        PO entity = CopyBean.copy(obj, PO.class);
        entityFacade.update(entity); 
        ResponseUtils.writeSuccessResult(response);
    }
    //@RequiresAuthentication
    @ApiOperation(value = "采购订单详情", httpMethod = "GET")
    @RequestMapping(value =  RoleAction.GET, method = RequestMethod.GET)
    public void view(@ApiParam(value = "键值") @RequestParam(value = "id", defaultValue = "0") long id,HttpServletResponse response)throws Exception{
        Employee entity = entityFacade.getById(Employee.class, id);
        if (null == entity) {
            throw new ParametersException("err.module.record.not_existed", "record not existed");
        }
        POVo vo = CopyBean.copy(entity, POVo.class);
        ResponseUtils.writeSuccessResult(response, vo);
    }

    //@RequiresAuthentication
    @RequestMapping(value = RoleAction.DELETE, method = RequestMethod.DELETE)
    @ApiOperation(value = "删除采购订单", httpMethod = "DELETE")
    public void del(@ApiParam(value = "主键", required = true) @PathVariable(value = "id") Long[] id, HttpServletResponse response) throws Exception {
        entityFacade.delete(Employee.class, id);
        ResponseUtils.writeSuccessResult(response);
    }

    //@RequiresAuthentication
    @RequestMapping(value = RoleAction.EXPORT, method = RequestMethod.GET)
    @ApiOperation(value = "导出采购订单", httpMethod = "GET")
    public void EXPORT(@ApiParam(value = "主键", required = true) @PathVariable(value = "id") Long[] id, HttpServletResponse response) throws Exception {
        entityFacade.delete(Employee.class, id);
        ResponseUtils.writeSuccessResult(response);
    }

    //@RequiresAuthentication
    @RequestMapping(value = RoleAction.IMPORT, method = RequestMethod.GET)
    @ApiOperation(value = "导入采购订单", httpMethod = "GET")
    public void IMPORT(@ApiParam(value = "主键", required = true) @PathVariable(value = "id") Long[] id, HttpServletResponse response) throws Exception {
        entityFacade.delete(Employee.class, id);
        ResponseUtils.writeSuccessResult(response);
    }

    //@RequiresAuthentication
    @RequestMapping(value = RoleAction.SUBMIT, method = RequestMethod.GET)
    @ApiOperation(value = "提交采购订单", httpMethod = "GET")
    public void SUBMIT(@ApiParam(value = "主键", required = true) @PathVariable(value = "id") Long[] id, HttpServletResponse response) throws Exception {
        entityFacade.delete(Employee.class, id);
        ResponseUtils.writeSuccessResult(response);
    }

    //@RequiresAuthentication
    @RequestMapping(value = RoleAction.CANCEL, method = RequestMethod.GET)
    @ApiOperation(value = "取消采购订单", httpMethod = "GET")
    public void CANCEL(@ApiParam(value = "主键", required = true) @PathVariable(value = "id") Long[] id, HttpServletResponse response) throws Exception {
        entityFacade.delete(Employee.class, id);
        ResponseUtils.writeSuccessResult(response);
    }

    //@RequiresAuthentication
    @RequestMapping(value = RoleAction.CONFIRM, method = RequestMethod.GET)
    @ApiOperation(value = "确认采购订单", httpMethod = "GET")
    public void CONFIRM(@ApiParam(value = "主键", required = true) @PathVariable(value = "id") Long[] id, HttpServletResponse response) throws Exception {
        entityFacade.delete(Employee.class, id);
        ResponseUtils.writeSuccessResult(response);
    }
}
